/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package models

import (
	"github.com/astaxie/beego/orm"
	"WebAdminSys/libs"
	"errors"
	"reflect"
	"strconv"
	"fmt"
)


type UserMailInfo struct {
  ID 					int64 `orm:"pk;auto"` 	//bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  UserID 				int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '推广号',
  GameID  				int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '推广号',
  Title  				string 					//varchar(128) NOT NULL DEFAULT '' COMMENT '邮件标题',
  Content 				string 					//varchar(512) NOT NULL DEFAULT '' COMMENT '邮件正文',
  Gold 					int64 					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '补偿金币',
  Status  				int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '邮件状态 0-未发送 1已发送 2-已领取 3-已删除',
  Gid 	 				int  					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '操作管理员',
  OpTime 				string 					//datetime NOT NULL COMMENT '发送时间',
  MailID 				int64					//bigint(20) DEFAULT '0' COMMENT '邮件ID',
}

type GMForbidUserRecord struct{
	ID 					int64 `orm:"pk;auto"` 
	USERID 				int64 `orm:"index"`
	GAMEID 				int64 `orm:"index"`  
	NICKNAME 			string   
	REGTIME 			int64  
	LASTLIGNTIME 		int64   
	ALLGOLD 			int64  
	ALLWIN 				int64  
	TYPE 				int   
	OPRATETIME 			int64  `orm:"index"`   
	CONTEXT 			string
	GMNAME 				string
	GMID  				int
}
type GMChangeCheatRecord struct{
	ID 					int64 `orm:"pk;auto"`
	USERID 				int64 `orm:"index"`
	GAMEID 				int64 `orm:"index"`
	NICKNAME 			string
	GMID 				int
	GMNAME 				string
	OldlCheatRate		int64
	OldlCheatLimit		int64
	NewlCheatLimit		int64
	NewlCheatRate		int64
	Writedate			int64
	LtotalScore			int64
	Reson				string
}


func UserInfoListData(page, pageSize int, strsql string) ([]orm.Params, int64,error) {
	offset := (page - 1) * pageSize
	o := orm.NewOrm(); 
	var maps []orm.Params  
 
	tmpsql := "SELECT UI.GAMEID AS A,cast(UI.NICKNAME as char) AS B,FROM_UNIXTIME(REGISTERTIME) AS C,FROM_UNIXTIME(LASTLOGINTIME) AS D,";
	tmpsql += "GOLD+INSUREGOLD AS zichan,TICKET AS E, UI.DIAMOND AS F,ALLWINLOSE AS G,TODAYWINLOSE AS H,ALLBUYSCORE AS I,";
	tmpsql += "UI.PLATFORM AS J,DEVICE AS K,UI.LVSIGN AS L,STATUS AS M,CHEATENDTIME AS N ,IFNULL(UP.userczje,0) AS USERALLC,UOI.ROOMID FROM UserInfo  AS UI ";
	tmpsql += "LEFT JOIN (SELECT IFNULL(SUM(RMB),0) AS userczje,GAMEID FROM  UserPurchaseInfo GROUP BY GAMEID) AS UP ON UI.GAMEID=UP.GAMEID ";
	tmpsql += "LEFT JOIN (SELECT IFNULL(ROOMID,-1) AS ROOMID,USERID FROM  UserOnlineInfo GROUP BY USERID) AS UOI ON UOI.USERID=UI.USERID WHERE 1=1 ";

	
	if strsql != ""{
		tmpsql = fmt.Sprintf("%s %s",tmpsql,strsql);
	}
	

	tmpsql += " ORDER BY zichan DESC,ALLWINLOSE DESC"

	tmpsql = fmt.Sprintf(" %s LIMIT %d,%d;",tmpsql,offset,pageSize);
	tmpSql2 := "SELECT COUNT(*) AS total  FROM UserInfo  AS UI  LEFT JOIN (SELECT IFNULL(SUM(RMB),0) AS userczje,GAMEID FROM  UserPurchaseInfo GROUP BY GAMEID) AS UP ON UI.GAMEID=UP.GAMEID "
	tmpSql2 += "LEFT JOIN (SELECT IFNULL(ROOMID,-1) AS ROOMID,USERID FROM  UserOnlineInfo GROUP BY USERID) AS UOI ON UOI.USERID=UI.USERID WHERE 1=1 ";
	tmpSql2 = fmt.Sprintf("%s %s",tmpSql2,strsql);
	 
	_,err := o.Raw(tmpsql).Values(&maps);
	var maps2 []orm.Params  
	total,err := o.Raw(tmpSql2).Values(&maps2);
	if len(maps2) >= 1{
		strtmp :=  maps2[0]["total"].(string);
		total,_ = strconv.ParseInt(strtmp, 10, 64)  
	}
	//fmt.Println("===========222222222",maps2, "maps =" ,len(maps2));
	//fmt.Println("===========",maps, "maps =" ,len(maps));
	return maps, total,err
}

func UserInfoTitleData( strsql string) ([]orm.Params, int64,error) {

	o := orm.NewOrm(); 
	var maps []orm.Params  
 
	tmpsql := "SELECT SUM(GOLD+INSUREGOLD) AS ALLGOLD,SUM(ALLWINLOSE) AS AAWL,SUM(TODAYWINLOSE) AS ATWL,SUM(UP.userczje) AS ACZ ";
	tmpsql += " FROM UserInfo  AS UI ";
	tmpsql += "LEFT JOIN (SELECT IFNULL(SUM(RMB),0) AS userczje,GAMEID FROM  UserPurchaseInfo GROUP BY GAMEID) AS UP ON UI.GAMEID=UP.GAMEID ";
	tmpsql += "LEFT JOIN (SELECT IFNULL(ROOMID,-1) AS ROOMID,USERID FROM  UserOnlineInfo GROUP BY USERID) AS UOI ON UOI.USERID=UI.USERID WHERE 1=1 ";
	
	tmpsql = fmt.Sprintf("%s %s",tmpsql,strsql);
	_,err := o.Raw(tmpsql).Values(&maps);

	//fmt.Println("===========222222222",maps, "maps =" ,len(maps));
	//fmt.Println("===========",maps, "maps =" ,len(maps));
	return maps, 0,err
}

func UserInfoDataByCondtion(strsql string) ([]orm.Params,error) {

	o := orm.NewOrm(); 
	var maps []orm.Params  
 
	tmpsql := "SELECT UI.GAMEID AS GAMEID,UI.USERID AS USERID,cast(UI.NICKNAME as char) AS NICKNAME,UI.ACCOUNT AS ACCOUNT,FROM_UNIXTIME(UI.REGISTERTIME) AS REGISTERTIME,"
	tmpsql += "UI.REGIESTIP AS REGIESTIP,FROM_UNIXTIME(UI.LASTLOGINTIME) AS LASTLOGINTIME,UI.LASTLOGINIP AS LASTLOGINIP,UI.MOBILE AS MOBILE,UI.GENDER AS GENDER,"
	tmpsql += "UI.GOLD AS GOLD,UI.INSUREGOLD AS INSUREGOLD,UI.GOLD+UI.INSUREGOLD AS zichan, IFNULL(UP.userczje,0) AS USERALLC,UI.ALLWINLOSE AS ALLWINLOSE,UI.ALLBUYSCORE AS ALLBUYSCORE,"
	tmpsql += "UI.ALLSELLSCORE AS ALLSELLSCORE,UI.TODAYWINLOSE AS TODAYWINLOSE,UI.TICKET AS TICKET,UI.DIAMOND AS DIAMOND,UI.LVSIGN AS LVSIGN,UI.STATUS AS STATUS,";
	tmpsql += "UI.USERLEVEL AS USERLEVEL,UI.EXP AS EXP,UI.VIPLEVEL AS VIPLEVEL,UI.VIPEXP AS VIPEXP,UI.DESCRIPTION AS DESCRIPTION, UI.MACHINE AS MACHINE,UI.PLATFORM AS PLATFORM,UI.DEVICE AS DEVICE,"
	tmpsql += "UI.CHEATRATE AS CHEATRATE,UI.CHEATENDTIME AS CHEATENDTIME ,UI.CHEATSCORE AS CHEATSCORE,UI.CHEATLEFTSCORE AS CHEATLEFTSCORE, UI.Is_follow AS Is_follow,UOI.ROOMID FROM UserInfo  AS UI ";
	tmpsql += "LEFT JOIN (SELECT IFNULL(SUM(RMB),0) AS userczje,GAMEID FROM  UserPurchaseInfo GROUP BY GAMEID) AS UP ON UI.GAMEID=UP.GAMEID ";
	tmpsql += "LEFT JOIN (SELECT IFNULL(ROOMID,-1) AS ROOMID,USERID FROM  UserOnlineInfo GROUP BY USERID) AS UOI ON UOI.USERID=UI.USERID WHERE 1=1 ";
	//充值等一下实验在钻石前面regdatetime

	
	
	tmpsql = fmt.Sprintf("%s %s",tmpsql,strsql);
	_,err := o.Raw(tmpsql).Values(&maps);

	fmt.Println("===========",maps, "maps =" ,len(maps));
	return maps,err
}
func  GetUserStatusInFo(roomID interface{}) string {
	if reflect.TypeOf(roomID) == nil{
		return "离线";
	}
	strtmp := roomID.(string);
	if strtmp == "0" {
		return "大厅"
	}
	iRoomID,err := strconv.Atoi(strtmp);
	if err != nil {
		return "错误";
	}

	query := orm.NewOrm().QueryTable(TableName("RoomCheatMain"))
	
	query = query.Filter("RoomID", iRoomID)

	var data RoomCheatMain;
	err  = query.One(&data, "RoomName");
	if err != nil{
		return "错误";
	}
	return data.RoomName;
	
}
func GetUserSex(data interface{}) string {
	strTmp := "0";
	if reflect.TypeOf(data) != nil{
		strTmp = data.(string)
	}else{
		return "error"
	}
	if strTmp == "0" {
		return "男";
	} else{
		return "女";
	}
	return "中";
}

func GetUserLockStats(data interface{}) (string,bool) {
	strTmp := "0";
	if reflect.TypeOf(data) != nil{
		strTmp = data.(string)
	}else{
		return "error",false;
	}
	if strTmp == "0" {
		return "正常",true;
	}else{
		return "锁定",false;
	}
	return "",false;
}

func GetUserContrlInFo(dataR,dataS,dataLS,DataCT,NewPj interface{}) (string,bool,string) {
	RateStr := "";
	if reflect.TypeOf(dataR) != nil{
		strTmp := dataR.(string)
		if strTmp != "0"{
			RateStr = "作弊率为：" + strTmp
			if reflect.TypeOf(dataS) != nil{
				RateStr += " 作弊分数："
				RateStr += dataS.(string)
			}else{
				RateStr += " 作弊分数："
				RateStr += 	"0";
			}

			if reflect.TypeOf(dataLS) != nil{
				RateStr += " 剩余作弊分数："
				RateStr += dataLS.(string)
			}else{
				RateStr += " 剩余作弊分数："
				RateStr += "0"
			}
		}else{
			RateStr = "未作弊"	
		}
	}else{
		RateStr = "error"
	}
	bFoolow := false;
	if reflect.TypeOf(NewPj) != nil{
		strTmp :=  NewPj.(string)
		if strTmp == "0"{
			bFoolow = true;
		}else{
			bFoolow = false;
			RateStr += "  关怀用户"
		}
	}
	
	DataStr := ""
	if reflect.TypeOf(DataCT) != nil{
		strTmp := DataCT.(string);
		if strTmp != "0" && strTmp != ""{
			DataStr = "  上次完成时间："+strTmp
		}
	}
	return RateStr,bFoolow,DataStr;
}

func  (ras *UserMailInfo)UserUserMailInfoInsertValue() ( error) {
	fmt.Println(ras);
	o:= orm.NewOrm();

	query :=o.QueryTable(TableName("UserMailInfo"))
	i, _ := query.PrepareInsert()

	_,err := i.Insert(ras);

	
	defer i.Close() // 别忘记关闭 statement

	return err
}


func RecordLoginData( page, pageSize int, strsql string) ([]orm.Params, int64,error) {

	o := orm.NewOrm(); 
	var maps []orm.Params  
 
	tmpsql := "SELECT UER.UserID AS UserID,UER.GameID AS GameID,cast(UER.NickName as char) AS NickName,UER.E_Type AS Tpye,UER.Gold AS Gold,UER.InsureGold AS InsureGold,"
	tmpsql += "(UER.Gold + UER.InsureGold) AS ZiChan,UER.Machine AS MAC,UER.ClientIP AS IP,from_unixtime(UER.EnterTime) AS ET,UER.ServerID AS SID,RCM.RoomName AS RoonName FROM UserEnterRecord AS UER "
	tmpsql += "LEFT JOIN (SELECT RoomName, RoomID FROM RoomCheatMain) AS RCM ON RCM.RoomID = UER.ServerID WHERE 1=1 "
	
	
	tmpsql = fmt.Sprintf("%s %s %s",tmpsql,strsql,"ORDER BY EnterTime DESC ");
	offset := (page - 1) * pageSize
	tmpsql = fmt.Sprintf(" %s LIMIT %d,%d;",tmpsql,offset,pageSize);
	_,err := o.Raw(tmpsql).Values(&maps);

	
	tmpsql2 := "SELECT COUNT(*) AS total FROM UserEnterRecord AS UER "
	tmpsql2 += "LEFT JOIN (SELECT RoomName, RoomID FROM RoomCheatMain) AS RCM ON RCM.RoomID = UER.ServerID WHERE 1=1 "
	tmpsql2 = fmt.Sprintf("%s %s",tmpsql2,strsql);

	var maps2 []orm.Params  
	total,err := o.Raw(tmpsql2).Values(&maps2);
	if len(maps2) >= 1{
		strtmp :=  maps2[0]["total"].(string);
		total,_ = strconv.ParseInt(strtmp, 10, 64)  
	}
	
	return maps,total,err
}


func RecordGoldChangeData( page, pageSize int, strsql string,userTableNames ...interface{}) ([]orm.Params, int64,error) {


	var maps []orm.Params
	iLenGth := len(userTableNames);
	if iLenGth < 1 {
		return maps,0,errors.New("表名错误！")
	}
	//fmt.Println("===========kkkkk",iLenGth);
	
	commonsqlstr := ""
	commonsqlstr = fmt.Sprintf(" SELECT * FROM %s WHERE 1=1 %s ",userTableNames[0].(string),strsql);
	if iLenGth > 1 {
		for k := 1; k < iLenGth; k ++ {
			commonsqlstr = fmt.Sprintf("%s UNION ALL SELECT * FROM %s WHERE 1=1 %s ",commonsqlstr,userTableNames[k].(string),strsql);
		}
	}

	o := orm.NewOrm(); 
	o.Using("dbgoldlog")

	
	tmpsql := "SELECT UAT.UserID AS UID,UAT.GameID AS GID,UAT.Memo AS Memo,UAT.ChangeGold AS ChangeGold,UAT.ChangeInsureGold AS CGold,";
	tmpsql += "UAT.Gold AS Gold,UAT.InsureGold AS InsureGold,(UAT.Gold  + UAT.InsureGold ) AS ZiChan,UAT.CheatRate AS CheatRate ,"
	tmpsql = fmt.Sprintf(" %s from_unixtime(UAT.ChangeTime) AS ChangeTime FROM ( %s ) AS UAT  ORDER BY ChangeTime DESC " ,tmpsql,commonsqlstr);
	offset := (page - 1) * pageSize
	tmpsql = fmt.Sprintf(" %s LIMIT %d,%d;",tmpsql,offset,pageSize);
	_,err := o.Raw(tmpsql).Values(&maps);

	
	tmpsql2 := fmt.Sprintf("SELECT COUNT(*) AS total  FROM ( %s ) AS UAT",commonsqlstr);

	var maps2 []orm.Params  
	total,err := o.Raw(tmpsql2).Values(&maps2);
	if len(maps2) >= 1{
		strtmp :=  maps2[0]["total"].(string);
		total,_ = strconv.ParseInt(strtmp, 10, 64)  
	}

	return maps,total,err
}
/*
"Y-m-d H:i:s"
"Y-m-d"
*/
func GetUserGoldChangeTableNames(befordate,enddate,FromMat string) ([]interface{}){

	userTableNames :=  make([]interface{}, 0);
	if GetUserGoldChangeConfTableName() == "0"{
		uTmp := "UserScoreChange1";
		bExist := CheckTableExist("dbgoldlog",uTmp)
		if bExist {
			userTableNames = append(userTableNames, uTmp);
		}
		return userTableNames;
	}
	if befordate == ""{
		befordate = libs.GetDateNowString()	
	} 
	if enddate == ""{
		enddate = libs.GetDateNowString()
	}
	iBefor,_ := libs.GetDateToInt(befordate,FromMat,"Ymd");
	iEnd,_	:=	libs.GetDateToInt(enddate,FromMat,"Ymd");
	if iBefor >= iEnd {
		uTmp := fmt.Sprintf("UserScoreChange_%d",iEnd);
		bExist := CheckTableExist("dbgoldlog",uTmp)
		if bExist {
			userTableNames = append(userTableNames, uTmp);
		}

	}else{
		for i := iEnd;i > (iBefor - 1); i--{
			uTmp := fmt.Sprintf("UserScoreChange_%d",i);
			bExist := CheckTableExist("dbgoldlog",uTmp)
			if bExist {
				userTableNames = append(userTableNames, uTmp);
			}

		}
	}

	return userTableNames;
}
func CheckTableExist(databasename,tablename string ) bool{
	o := orm.NewOrm(); 
	o.Using(databasename);
	sqlstr := "";
	sqlstr = fmt.Sprintf("SHOW TABLES LIKE '%s'", tablename);
	var maps []orm.Params
	o.Raw(sqlstr).Values(&maps);
	//fmt.Println("xxxxxxxxxxxxxxxx",maps);
	if len(maps) > 0{
		return true
	}
	return false;
}



func RecordChargeData( page, pageSize int, strsql string) ([]orm.Params, int64,error) {


	var maps []orm.Params
	
	
	commonsqlstr := "SELECT  USERID,GAMEID,cast(NICKNAME as char) AS NICKNAME,RMB,SCORE,from_unixtime(PURCHASETIME) AS PURCHASETIME,PLATFORM,LVSIGN FROM UserPurchaseInfo WHERE 1=1 "
	commonsqlstr = fmt.Sprintf(" %s %s ",commonsqlstr,strsql);

	o := orm.NewOrm(); 

	offset := (page - 1) * pageSize
	commonsqlstr = fmt.Sprintf("%s ORDER BY PURCHASETIME DESC LIMIT %d,%d;",commonsqlstr,offset,pageSize);
	_,err := o.Raw(commonsqlstr).Values(&maps);

	
	tmpsql2 := "SELECT COUNT(*) AS total  FROM UserPurchaseInfo WHERE 1=1"
	tmpsql2 = fmt.Sprintf("%s %s",tmpsql2,strsql);
	var maps2 []orm.Params  
	total,err := o.Raw(tmpsql2).Values(&maps2);
	if len(maps2) >= 1{
		strtmp :=  maps2[0]["total"].(string);
		total,_ = strconv.ParseInt(strtmp, 10, 64)  
	}

	return maps,total,err
}
func RecordChargeDataTitle() ([]orm.Params, int64,error) {

	var maps []orm.Params
	commonsqlstr := "select SUM(RMB) AS RMB,COUNT(DISTINCT(GAMEID)) AS ALLPRUCOUNT FROM UserPurchaseInfo;"
	o := orm.NewOrm(); 
	_,err := o.Raw(commonsqlstr).Values(&maps);

	return maps,0,err
}
func  (ras *GMForbidUserRecord)GMForbidUserRecordInsertValue() ( error) {

	strsql := fmt.Sprintf(" AND UI.GameID = %d",ras.GAMEID);
	ret,err:= UserInfoDataByCondtion(strsql);
	if len(ret) > 0 && err == nil {
		strtmp :=  ret[0]["GAMEID"].(string);
		ras.GAMEID,_ = strconv.ParseInt(strtmp, 10, 64);
		
		strtmp =  ret[0]["USERID"].(string);
		ras.USERID,_ = strconv.ParseInt(strtmp, 10, 64);

		strtmp =  ret[0]["NICKNAME"].(string);
		ras.NICKNAME = strtmp

		strtmp =  ret[0]["REGISTERTIME"].(string);
		ras.REGTIME = libs.GetStringTimeToUnix(strtmp,"Y-m-d H:i:s")
	
		strtmp =  ret[0]["LASTLOGINTIME"].(string);
		ras.LASTLIGNTIME = libs.GetStringTimeToUnix(strtmp,"Y-m-d H:i:s")
		
		strtmp =  ret[0]["zichan"].(string);
		ras.ALLGOLD,_ = strconv.ParseInt(strtmp, 10, 64);

		strtmp =  ret[0]["ALLWINLOSE"].(string);
		ras.ALLWIN,_ = strconv.ParseInt(strtmp, 10, 64);
	}
	ras.OPRATETIME = libs.GetCurSysTmeTimeToUnix();
		
	o:= orm.NewOrm();
	query :=o.QueryTable(TableName("GMForbidUserRecord"))
	InsertTable, _ := query.PrepareInsert()
	_,err= InsertTable.Insert(ras);

	defer InsertTable.Close() // 别忘记关闭 statement

	return err
}

func RecordLockData( page, pageSize int, strsql string) ([]orm.Params, int64,error) {


	tmpsql := "SELECT USERID,GAMEID,RealName,cast(NICKNAME as char) AS NICKNAME,from_unixtime(REGTIME) AS REGTIME, from_unixtime(LASTLIGNTIME) AS LASTLIGNTIME,"
	tmpsql += " ALLGOLD,ALLWIN,TYPE,from_unixtime(OPRATETIME) AS OPRATETIME, cast(CONTEXT as char) AS CONTEXT,cast(GMNAME as char) AS GMNAME,GMID "
	
	offset := (page - 1) * pageSize
	
	tmpsql = fmt.Sprintf(" %s FROM GMForbidUserRecord AS C INNER JOIN ManageAdminUser AS G ON C.GMID=G.Id WHERE 1=1 %s ORDER BY OPRATETIME DESC  LIMIT %d,%d; " ,tmpsql,strsql,offset,pageSize);
	
	var maps []orm.Params
	o := orm.NewOrm(); 
	_,err := o.Raw(tmpsql).Values(&maps);

	
	tmpsql2 := fmt.Sprintf("SELECT COUNT(*)  AS total FROM GMForbidUserRecord WHERE 1=1  %s",strsql);

	var maps2 []orm.Params  
	total,err := o.Raw(tmpsql2).Values(&maps2);
	if len(maps2) >= 1{
		strtmp :=  maps2[0]["total"].(string);
		total,_ = strconv.ParseInt(strtmp, 10, 64)  
	}

	return maps,total,err
}




func  (ras *GMChangeCheatRecord)GMChangeCheatRecordInsertValue(useroldInfo []orm.Params) ( error) {


	if len(useroldInfo) > 0  {

		ras.GAMEID= useroldInfo[0]["GAMEID"].(int64); 
		ras.USERID = useroldInfo[0]["USERID"].(int64);

		ras.NICKNAME =  useroldInfo[0]["NICKNAME"].(string);
		ras.OldlCheatRate =  useroldInfo[0]["CHEATRATE"].(int64);
	
	
		ras.OldlCheatLimit =  useroldInfo[0]["CHEATSCORE"].(int64);
		
		ras.LtotalScore =  useroldInfo[0]["ALLWINLOSE"].(int64);
	
	}
	ras.Writedate = libs.GetCurSysTmeTimeToUnix();
		
	o:= orm.NewOrm();
	query :=o.QueryTable(TableName("GMChangeCheatRecord"))
	InsertTable, _ := query.PrepareInsert()
	_,err := InsertTable.Insert(ras);

	defer InsertTable.Close() // 别忘记关闭 statement

	return err
}

func RecordMailData( page, pageSize int, strsql string) ([]orm.Params, int64,error) {
	tmpsql := "SELECT M.ID,(M.UserID) AS MUID,(M.GameID) AS MGAMEID,cast(U.NICKNAME AS char) AS USERNAME,Title,Content,"
	tmpsql += "M.Gold AS MGOLD,M.Gid AS MGID,OpTime,RealName,M.`Status` AS MStatus FROM UserMailInfo AS M "
	tmpsql += "INNER JOIN UserInfo AS U ON M.UserID=U.USERID INNER JOIN ManageAdminUser AS G ON M.Gid=G.Id WHERE M.`Status`<>3"
	offset := (page - 1) * pageSize
	tmpsql = fmt.Sprintf(" %s %s ORDER BY OpTime DESC  LIMIT %d,%d; " ,tmpsql,strsql,offset,pageSize);
	var maps []orm.Params
	o := orm.NewOrm(); 
	_,err := o.Raw(tmpsql).Values(&maps);

	tmpsql2 := fmt.Sprintf("SELECT COUNT(*)  AS total  FROM UserMailInfo AS M INNER JOIN UserInfo AS U ON M.UserID=U.USERID INNER JOIN ManageAdminUser AS G ON M.Gid=G.Id WHERE M.`Status`<>3 %s",strsql);

	var maps2 []orm.Params  
	total,err := o.Raw(tmpsql2).Values(&maps2);
	if len(maps2) >= 1{
		strtmp :=  maps2[0]["total"].(string);
		total,_ = strconv.ParseInt(strtmp, 10, 64)  
	}

	return maps,total,err

}

func RecordMailDataTitle( strsql string) ([]orm.Params,error){
	
	tmpsql := "SELECT IFNULL(SUM(M.Gold),0) AS MGOLD,COUNT(*) AS total FROM UserMailInfo AS M "
	tmpsql += "INNER JOIN UserInfo AS U ON M.UserID=U.USERID INNER JOIN ManageAdminUser AS G ON M.Gid=G.Id WHERE M.`Status`<>3"

	tmpsql = fmt.Sprintf(" %s %s " ,tmpsql,strsql);
	var maps []orm.Params
	o := orm.NewOrm(); 
	_,err := o.Raw(tmpsql).Values(&maps);

	return maps,err

}
func RecordCtrlData( page, pageSize int, strsql string) ([]orm.Params, int64,error) {
 // AND RealName LIKE '%超%'

	tmpsql := "SELECT USERID,GAMEID,cast(NICKNAME as char) AS NICKNAME,RealName,OldlCheatRate,OldlCheatLimit,"
	tmpsql += "NewlCheatLimit,NewlCheatRate,from_unixtime(Writedate) AS Writedate,LtotalScore,cast(Reson as char) AS Reson FROM GMChangeCheatRecord AS C "
	tmpsql += "INNER JOIN ManageAdminUser AS G ON C.GMID=G.Id WHERE 1=1 "
	
	offset := (page - 1) * pageSize
	
	tmpsql = fmt.Sprintf(" %s %s ORDER BY Writedate DESC  LIMIT %d,%d; " ,tmpsql,strsql,offset,pageSize);
	
	var maps []orm.Params
	o := orm.NewOrm(); 
	_,err := o.Raw(tmpsql).Values(&maps);

	
	tmpsql2 := fmt.Sprintf("SELECT COUNT(*)  AS total  FROM GMChangeCheatRecord AS C INNER JOIN ManageAdminUser AS G ON C.GMID=G.Id WHERE 1=1 %s",strsql);

	var maps2 []orm.Params  
	total,err := o.Raw(tmpsql2).Values(&maps2);
	if len(maps2) >= 1{
		strtmp :=  maps2[0]["total"].(string);
		total,_ = strconv.ParseInt(strtmp, 10, 64)  
	}

	return maps,total,err
}



	

func RecordCtrlDataTitle( strsql string) ([]interface{},error){
 // AND RealName LIKE '%超%'
//SELECT SUM(NewlCheatLimit) FROM tmp_RateLog WHERE NewlCheatRate<0; 
	//SELECT SUM(NewlCheatLimit) FROM tmp_RateLog WHERE NewlCheatRate>0; 
	UserWinLoseSocre :=  make([]interface{}, 0);
	tmpsql := "SELECT  SUM(NewlCheatLimit) AS USERSCORE FROM GMChangeCheatRecord AS C INNER JOIN ManageAdminUser AS G ON C.GMID=G.Id WHERE 1=1 "
	

	
	tmpsql1 := fmt.Sprintf(" %s %s  AND NewlCheatRate < 0" ,tmpsql,strsql);
	
	var maps []orm.Params
	o := orm.NewOrm(); 
	_,err := o.Raw(tmpsql1).Values(&maps);

	if len(maps) >= 1{
		strtmp :=  maps[0]["USERSCORE"].(string);
		UserWinLoseSocre = append(UserWinLoseSocre,strtmp)
	}

	tmpsql2 := fmt.Sprintf(" %s %s  AND NewlCheatRate > 0" ,tmpsql,strsql); 
	var maps2 []orm.Params 

	_,err = o.Raw(tmpsql2).Values(&maps2);
	if len(maps2) >= 1{
		strtmp :=  maps2[0]["USERSCORE"].(string);
		UserWinLoseSocre = append(UserWinLoseSocre,strtmp)
	}

	return UserWinLoseSocre,err
}


